const Cesium = window.Cesium;

import { CreatePosition, formarColor } from "../../Config";

class Satellite {
    constructor(Entity, platform, parentOptions = {}) {
        if (!Entity) {
            throw new Error('至少传入一个 Entity 对象');
        };
        this.FontSize = window.REMFontSize || 1;
        this.Entity = Entity;
        this.platform = platform.BaseEntity;
        this.parentOptions = parentOptions;
        this.color = formarColor(this.parentOptions.color) || Cesium.Color.AQUAMARINE;
    }
    set show(type) {
        if (this.Entity.ellipse) {
            this.Entity.ellipse.show = !!type;
        };
        if (this.Entity.cylinder) {
            this.Entity.cylinder.show = !!type;
        };
    }
    get show() {
        if (this.Entity.cylinder) {
            return this.Entity.cylinder.show
        };
        if (this.Entity.ellipse) {
            return this.Entity.ellipse.show
        }
    }
    set Color(value) {
        this.color = formarColor(value);
        var _color = formarColor(value);
        if (this.Entity.ellipse) {
            this.Entity.ellipse.material = _color.withAlpha(0.3);
        } else {
            this.Entity.cylinder.material = _color.withAlpha(0.3);
        }
    }
    get Color() {
        return this.color
    }
    changeSenceType(type) {
        if (type == '3D') {
            this.Entity.ellipse = undefined;
            this.Entity.cylinder = this.render3DRadar();
        } else {
            this.Entity.ellipse = this.render2DRadar()
            this.Entity.cylinder = undefined;
        }
    }
    changeRadius(radius) {
        this.radius = radius;
        if (this.Entity.ellipse) {
            this.Entity.ellipse.semiMajorAxis = radius;
            this.Entity.ellipse.semiMinorAxis = radius;
        } else {
            this.Entity.cylinder.radii = new Cesium.Cartesian3(radius, radius, radius);
        }
    }
    render2DRadar() {
        return {
            height: 0,
            material: this.color.withAlpha(0.5),
            semiMajorAxis: 12000,
            semiMinorAxis: 12000,
        }
    }
    render3DRadar() {
        var position = this.platform.Position;
        var satelliteCenter = this.platform.CesiumPosition;
        var cylinderLength = Cesium.Cartesian3.distance(satelliteCenter, CreatePosition({
            latitude: position.latitude,
            longitude: position.longitude,
            height: 0
        }));
        var cylinderPosition = Cesium.Cartesian3.midpoint(satelliteCenter, CreatePosition({
            latitude: position.latitude,
            longitude: position.longitude,
            height: 0
        }), new Cesium.Cartesian3);
        this.Entity.position.setValue(cylinderPosition);
        return {
            length: cylinderLength,
            topRadius: 0,
            bottomRadius: 120000,
            material: this.color.withAlpha(0.5),
        }
    }
}



export { Satellite }
